package org.itboys.mobile.service;

import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.TianJieWalletBlockRecordMapper;
import org.itboys.mobile.dao.TianJieWalletRecordMapper;
import org.itboys.mobile.entity.mysql.TianJieWallet;
import org.itboys.mobile.entity.mysql.TianJieWalletBlock;
import org.itboys.mobile.entity.mysql.TianJieWalletBlockRecord;
import org.itboys.mobile.entity.mysql.TianJieWalletRecord;
import org.itboys.mobile.util.DatetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import java.math.BigDecimal;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_后台首页_service
 */
@Service
public class SundayDesktopService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private TianJieWalletRecordMapper walletRecordMapper;
    @Autowired
    private TianJieWalletBlockRecordMapper walletBlockRecordMapper;

    /**
     * 获取后台首页数据
     */
    public Map<String,Object> getDesktopData(){
        Map<String,Object> result = new HashMap<String,Object>();
        Double charge=0.00;//总充值记录
        Double todayCharge=0.00;//今日充值
        Double buy=0.00;//今日区块链买入
        Double todayBuy=0.00;//区块链买入
        Double sale=0.00;//区块链卖出
        Double todaySale=0.00;//今日区块链卖出
        Double cash=0.00;//提币（提现）
        Double todayCash=0.00;//今日提币（提现）
        long todayStart =DatetUtil.setDateStart(new Date()).getTime();
        long todayEnd =DatetUtil.setDateEnd(new Date()).getTime();

        Map<String,Object> param = new HashMap<>();
        param.put("types", Arrays.asList(SundayCommonConstants.wallet_record_type_charge,SundayCommonConstants.wallet_record_type_cash));
        //充值，提现
        List<TianJieWalletRecord> walletRecords=walletRecordMapper.select(param);
        for(TianJieWalletRecord walletRecord:walletRecords){
            //充值
            if(walletRecord.getType()== SundayCommonConstants.wallet_record_type_charge&&
                    walletRecord.getStatus()==SundayCommonConstants.YES){
                charge+=walletRecord.getAmount().doubleValue();
                //今日充值
                if(walletRecord.getCt().getTime()>=todayStart&&walletRecord.getCt().getTime()<=todayEnd){
                    todayCharge+=walletRecord.getAmount().doubleValue();
                }
            }
            //提币（提现）
            if(walletRecord.getType()== SundayCommonConstants.wallet_record_type_cash){
                cash+=walletRecord.getAmount().doubleValue();
                //今日充值
                if(walletRecord.getCt().getTime()>=todayStart&&walletRecord.getCt().getTime()<=todayEnd){
                    todayCash+=walletRecord.getAmount().doubleValue();
                }
            }


        }
        param.clear();
        param.put("types", Arrays.asList(SundayCommonConstants.block_record_type_buy,SundayCommonConstants.block_record_type_sale));
        //买入，卖出，
        List<TianJieWalletBlockRecord> walletBlockRecords=walletBlockRecordMapper.select(param);
        for(TianJieWalletBlockRecord walletBlockRecord:walletBlockRecords){
            //买入
            if(walletBlockRecord.getType()== SundayCommonConstants.block_record_type_buy){
                buy+=walletBlockRecord.getAmount().doubleValue();
                //今日买入
                if(walletBlockRecord.getCt().getTime()>=todayStart&&walletBlockRecord.getCt().getTime()<=todayEnd){
                    todayBuy+=walletBlockRecord.getAmount().doubleValue();
                }
            }
            //卖出
            if(walletBlockRecord.getType()== SundayCommonConstants.block_record_type_sale){
                sale+=walletBlockRecord.getAmount().doubleValue();
                //今日卖出
                if(walletBlockRecord.getCt().getTime()>=todayStart&&walletBlockRecord.getCt().getTime()<=todayEnd){
                    todaySale+=walletBlockRecord.getAmount().doubleValue();
                }
            }

        }
        result.put("charge",new BigDecimal(charge).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("todayCharge",new BigDecimal(todayCharge).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("buy",new BigDecimal(buy).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("todayBuy",new BigDecimal(todayBuy).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("sale",new BigDecimal(sale).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("todaySale",new BigDecimal(todaySale).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("cash",new BigDecimal(cash).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("todayCash",new BigDecimal(todayCash).setScale(2,BigDecimal.ROUND_HALF_UP));
        return  result;



    }


}
